﻿using AbpIntegrationDemo.Api.Domains.OrderLogs;
using AbpIntegrationDemo.Api.Domains.Orders;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Timing;

namespace AbpIntegrationDemo.Api.Applications.Orders.Mutations;

[ExtendObjectType("Mutation")]
public class OrderMutationResolver
{
    public async Task<Guid> CreateOrder(string name,
        [Service] IRepository<Order, Guid> orderRepository,
        [Service] IRepository<OrderLog, Guid> orderLogRepository,
        [Service] IClock clock)
    {
        var order = new Order()
        {
            Name = name
        };
        order = await orderRepository.InsertAsync(order, autoSave: true);

        var orderLog = new OrderLog()
        {
            OrderId = order.Id,
            CreateTime = clock.Now
        };
        await orderLogRepository.InsertAsync(orderLog, autoSave: true);

        if (name.Contains("exception"))
        {
            throw new Exception("exception test");
        }

        return order.Id;
    }
}
